package com.agger.harmonyosthemeseditor.intelligent;

import java.io.*;
import java.util.zip.*;

public class ZipFileTool
{
    public static void zip(String src, String dest) throws IOException
    {
        ZipOutputStream out = null;
        out.setMethod(ZipOutputStream.DEFLATED);
        try
        {
            File outFile = new File(dest);
            File fileOrDirectory = new File(src);
            out = new ZipOutputStream(new FileOutputStream(outFile));
            if (fileOrDirectory.isFile())
            {
                zipFileOrDirectory(out, fileOrDirectory, "");
            } else
            {
                File[] entries = fileOrDirectory.listFiles();
                for (File entry : entries)
                    zipFileOrDirectory(out, entry, "");
            }
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } finally {
            if (out != null)
            {
                try
                {
                    out.close();
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

    private static void zipFileOrDirectory(ZipOutputStream out,File fileOrDirectory, String curPath) throws IOException
    {
        FileInputStream in = null;
        try
        {
            if (!fileOrDirectory.isDirectory())
            {
                byte[] buffer = new byte[4096];
                int bytes_read;
                in = new FileInputStream(fileOrDirectory);
                ZipEntry entry = new ZipEntry(curPath+fileOrDirectory.getName());
                out.putNextEntry(entry);
                while ((bytes_read = in.read(buffer)) != -1)
                {
                    out.write(buffer, 0, bytes_read);
                }
                out.closeEntry();
            } else
            {
                File[] entries = fileOrDirectory.listFiles();
                for (File entry : entries)
                    zipFileOrDirectory(out, entry, curPath + fileOrDirectory.getName() + "/");
            }
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } finally
        {
            if (in != null)
            {
                try
                {
                    in.close();
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

    public static boolean unzip(String zippath ,String outzippath)
    {
        try
        {
            File file = new File(zippath);
            File outFile;
            ZipFile zipFile = new ZipFile(file);
            ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry;
            InputStream input;
            OutputStream output;
            while((entry = zipInput.getNextEntry()) != null)
            {
                outFile = new File(outzippath + File.separator + entry.getName());
                if(!outFile.getParentFile().exists()) {outFile.getParentFile().mkdir();}
                if(!entry.isDirectory())
                {
                    if(!outFile.exists()) {outFile.createNewFile();}
                    else if(outFile.exists()) {outFile.delete();}
                    input = zipFile.getInputStream(entry);
                    output = new FileOutputStream(outFile);
                    int temp;
                    while((temp = input.read())!=-1){output.write(temp);}
                    input.close();
                    output.close();
                }
                else if(entry.isDirectory())
                {
                    if(!outFile.exists()) {outFile.mkdirs();}
                }

            }return true;
        } catch (Exception e) {return false;}
    }

}
